/*
Copyright (C) 2005 t-arn.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
See the GNU General Public License for more details. 

*/

package com.t_arn;
import ewex.registry.Registry;
import ewex.registry.RegistryKey;
import ewe.util.mString;
import ewe.util.SubString;
import ewe.filechooser.FileChooser;

//##################################################################
/** This class provides high level registry functions.
 * If you want to use it for your own programs, you should extend it and override the private static String stVersion.
 * 
 * <pre>Examples:
 * import com.t_arn.taRegistry;
 * stProgramDir = taRegistry.getProgramDir("t-arn.com","KaffWrapper");
 * stKaffExe = taRegistry.getProgramValue("t-arn.com","KaffWrapper", "KaffExe");
 * taRegistry.setProgramValue("t-arn.com","KaffWrapper", "KaffExe", "c:\\kaffe\\kaff.exe");
 * stFileType = taRegistry.getValue("HKEY_CLASSES_ROOT\\.html", "");  // Get default value
 * taRegistry.setValue("HKEY_CLASSES_ROOT\\.html", "", "htmlfile");   // Set default value
 * taRegistry.setProgramDir("t-arn.com","KaffWrapper", "c:\\programs\\kaffwrapper\\");
 * taRegistry.setProgramDir("t-arn.com","KaffWrapper", "");  // prompt for ProgramDir
 * taRegistry.shellOpen("c:\\Programs\\KaffWrapper\\KaffWrapper.html");
 * </pre>
 * 
 * @since 02.12.04 (version 1.1.1) - 29.12.05 (version 1.2)
 * @author Tom Arn
 * @author www.t-arn.com
 * @version 1.2
 */
public class taRegistry
//##################################################################
{
	private static String stVersion = "1.2";
	/** This is the parent key, under which all programs store their stuff.
	 */
	private static String stRegBase = "HKEY_CURRENT_USER\\Software\\";

//===================================================================
/** Constructor. Is normally not used since all methods are static.
 */
public taRegistry ()
//===================================================================
{

}
//===================================================================
/** Returns the version of the class.
 * 
 * @return The version of this class
 */
public static String getVersion ()
//===================================================================
{
	return stVersion;
}
/** 
 * This fixes a bug in Ewe 1.43 ewex.registry.Registry.
 * Get a key in the local registry. If the path or root specified is invalid for any reason, it will return null. If the ewex_registry.dll could not be loaded, it will return null.
 * <p>
 * Note that the path MUST be separated by '\' characters and NOT '/' characters.
 * <p>
 * If a root of 0 is used, it will be assumed that the path will start with "HKEY_CLASSES_ROOT\..." or one of the other text representation of the roots. These roots are:
 * "HKEY_CLASSES_ROOT","HKEY_CURRENT_USER","HKEY_LOCAL_MACHINE","HKEY_USERS","HKEY_CURRENT_CONFIG","HKEY_DYN_DATA"
 * 
 * @param root 
 * @param path 
 * @param fullAccess 
 * @param create 
 */
//===================================================================
public static RegistryKey getLocalKey(int root, String path, boolean fullAccess, boolean create)
//===================================================================
{
	// fixes a bug in Vm version 1.43
	
	if ( ewe.sys.Vm.getVersion() == 143 )
	{
		if (root == 0){
			StringBuffer sb = new StringBuffer();
			root = Registry.toRootAndPath(path,sb);
			path = sb.toString();
		}
	}
	return Registry.getLocalKey(root,path,fullAccess,create);
}

//===================================================================
/** Reads a text value from the registry.
 * 
 * @param stKey The key name, e.g. "HKEY_CLASSES_ROOT\\.html"
 * @param stName The value name. Specify an empty string "" for the default
 *      value.
 * @return The text value or an empty string, if the value does not exist.
 */
public static String getValue (String stKey, String stName)
//===================================================================
{
	Object obj;
	String stValue = "";
	RegistryKey regKey;

	regKey = taRegistry.getLocalKey(0,stKey,false,false);
	if (regKey != null)
	{
		obj = regKey.getValue(stName);
		if (obj == null) stValue = "";
		else stValue = obj.toString();
	}
	return stValue;
}
//===================================================================
/** Reads a text value for a certain application of a certain company from the registry. <br>
 * stProg defines the name of a child key below "\\HKEY_CURRENT_USER\\Software\\(company)"
 * <p>
 * Example: <br>
 * taRegistry.getProgramValue("tanapro.ch","taRadiusSrv", "Test") <br>
 * returns the text value "Test" from the key "\\HKEY_CURRENT_USER\\Software\\tanapro.ch\\taRadiusSrv"
 * 
 * @param stCompany The company name.
 * @param stProg The application name.
 * @param stName The value name. Specify an empty string "" for the default
 *      value.
 * @return The text value or an empty string, if the value does not exist.
 */
public static String getProgramValue (String stCompany, String stProg, String stName)
//===================================================================
{
	return taRegistry.getValue(stRegBase+stCompany+"\\"+stProg, stName);
}
//===================================================================
/** see description of getProgramValue (String,String,String)<br>
 * stCompany is set to "t-arn.com"
 * 
 * @param stProg The application name.
 * @param stName The value name. Specify an empty string "" for the default
 *      value.
 * @return The text value or an empty string, if the value does not exist.
 */
public static String getProgramValue (String stProg, String stName)
//===================================================================
{
	return taRegistry.getValue(stRegBase+"t-arn.com\\"+stProg, stName);
}
//===================================================================
/** Reads the text value ProgramDir for a certain application from the registry. Directory names always end with a backslash.
 * stProg defines the name of a child key below "\\HKEY_CURRENT_USER\\Software\\t-arn.com"
 * <p>
 * Example: <br>
 * taRegistry.getProgramDir("SmtpMail") <br>
 * returns the text value "ProgramDir" from the key "\\HKEY_CURRENT_USER\\Software\\t-arn.com\\SmtpMail"
 * 
 * @param stProg The application name.
 * @return The text value ProgramDir or an empty string, if the value does not
 *      exist.
 */
public static String getProgramDir (String stProg)
//===================================================================
{
	return taRegistry.getProgramValue(stProg, "ProgramDir");
}
//===================================================================
/** Writes a text value to the registry. If the key stKey does not exist, it is created.
 * 
 * @param stKey The key name, e.g. "HKEY_CLASSES_ROOT\\.d"
 * @param stName The value name. Specify an empty string "" for the default
 *      value.
 * @param stValue The text string (content) of the value.
 */
public static void setValue (String stKey, String stName, String stValue)
//===================================================================
{
	boolean bOK;
	RegistryKey regKey;

	regKey = taRegistry.getLocalKey(0,stKey,true,true);
	if (regKey == null) new ewe.ui.MessageBox("taRegistry.setValue()", "Error creating registry key", ewe.ui.MessageBox.MBOK).execute();
	else
	{
		bOK = regKey.setValue(stName, stValue);
		if (!bOK) new ewe.ui.MessageBox("taRegistry.setValue()", "Error writing registry value", ewe.ui.MessageBox.MBOK).execute();
	}
}
//===================================================================
/** Writes a text value for a certain application of a certain company to the registry. If the key stKey does not exist, it is created.
 * <p>
 * Example: <br>
 * taRegistry.setProgramValue("tanapro.ch","taRadiusSrv", "Test", "This is a test.") <br>
 * writes the text "This is a test." into the value "Test" of the key <br>
 * "\\HKEY_CURRENT_USER\\Software\\tanapro.ch\\taRadiusSrv"
 * 
 * @param stCompany The company name.
 * @param stProg The application name.
 * @param stName The value name. Specify an empty string "" for the default
 *      value.
 * @param stValue The text string (content) of the value.
 */
public static void setProgramValue (String stCompany, String stProg, String stName, String stValue)
//===================================================================
{
	taRegistry.setValue(stRegBase+stCompany+"\\"+stProg,stName,stValue);
}
//===================================================================
/** See description of setProgramValue (String,String,String) <br>
 * stCompany is set to "t-arn.com"
 * 
 * @param stProg The application name.
 * @param stName The value name. Specify an empty string "" for the default
 *      value.
 * @param stValue The text string (content) of the value.
 */
public static void setProgramValue (String stProg, String stName, String stValue)
//===================================================================
{
	taRegistry.setValue(stRegBase+"t-arn.com\\"+stProg,stName,stValue);
}
//===================================================================
/** Writes the text value ProgramDir for a certain application to the registry. If the key stProg does not exist, it is created.
 * <p>
 * Example:<br>
 * taRegistry.setProgramDir("SmtpMail", "C:\\tools\smtpmail") <br>
 * writes the directory name "C:\\tools\smtpmail\\" into the value "ProgramDir" of the key "\\HKEY_CURRENT_USER\\Software\\t-arn.com\\SmtpMail"
 * 
 * @param stProg The application name.
 * @param stDir The directory name to store. If the backslash at the end is
 *      missing, it is added automatically.
 */
public static void setProgramDir (String stProg, String stDir)
//===================================================================
{
	if (stDir.equals("")) {
		FileChooser fc = new FileChooser(FileChooser.DIRECTORY_SELECT, "\\");
		fc.title = "Choose program dir of '"+stProg+"'";
		if (fc.execute() == FileChooser.IDOK) stDir = fc.getChosen();
		else return;
		if (!stDir.endsWith("\\")) stDir = stDir+"\\";
	}
	taRegistry.setProgramValue(stProg, "ProgramDir", stDir);
}
//===================================================================
/** Opens the file with the associated application.
 * <p>
 * Example: <br>
 * taRegistry.shellOpen("C:\tPad\tPad.html") <br>
 * shows the file "C:\tPad\tPad.html" with the associated application (probably the Internet Explorer).
 * 
 * @param stFileName The complete path and name of the file.
 */
public static void shellOpen (String stFileName)
//===================================================================
{
	String stFileType, st1, stDelim="%1", stCmd, stParams="";
	SubString sub = new SubString();
	String stExt = mString.rightOf(stFileName, '.');
	int i;

	if (stExt.equals("")) new ewe.ui.MessageBox("taRegistry.shellOpen()", "No file extension found", ewe.ui.MessageBox.MBOK).execute();
	else
	{
		stFileType = taRegistry.getValue("HKEY_CLASSES_ROOT\\."+stExt, "");
		if (stFileType.equals("")) new ewe.ui.MessageBox("taRegistry.shellOpen()", "Extension "+stExt+ " not found in Registry", ewe.ui.MessageBox.MBOK).execute();
		else
		{
			st1 = taRegistry.getValue("HKEY_CLASSES_ROOT\\"+stFileType+"\\Shell\\Open\\Command", "");
			if (st1.equals("")) new ewe.ui.MessageBox("taRegistry.shellOpen()", "No Association for extension "+stExt+" found in Registry", ewe.ui.MessageBox.MBOK).execute();
			else
			{
				sub.set(st1);
				sub.replace(stDelim.toCharArray(), stFileName.toCharArray());
				st1 = sub.toString();
				i = 0;
				if (st1.charAt(0)=='"') i = st1.indexOf(34,1)+1;
				i = st1.indexOf(32,i);
				if (i!=-1)  // char not found (i==-1) means no parameters available
				{
					stCmd = st1.substring(0,i);
					if (st1.length()>i+1) stParams = st1.substring (i+1);
				}
				else stCmd = st1.toString();
				ewe.sys.Vm.exec(stCmd, stParams, 0, false);
			}
		}
	}
}
//===================================================================
}
//##################################################################
